package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.ClaimDelayinsurance;
import com.ktgj.customer.service.IClaimDelayinsuranceService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 保险理赔合并
Controller
 * 
 * @author ruoyi
 * @date 2022-06-17
 */
@RestController
@RequestMapping("/delayinsurance")
public class ClaimDelayinsuranceController extends BaseController
{
    @Autowired
    private IClaimDelayinsuranceService claimDelayinsuranceService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询保险理赔合并
列表
     */
    @PreAuthorize(hasPermi = "system:delayinsurance:list")
    @GetMapping("/list")
    public TableDataInfo list(ClaimDelayinsurance claimDelayinsurance)
    {
        startPage();
        List<ClaimDelayinsurance> list = claimDelayinsuranceService.selectClaimDelayinsuranceList(claimDelayinsurance);
        return getDataTable(list);
    }

    /**
     * 导出保险理赔合并
列表
     */
    @PreAuthorize(hasPermi = "system:delayinsurance:export")
    @Log(title = "保险理赔合并", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClaimDelayinsurance claimDelayinsurance) throws IOException
    {
        List<ClaimDelayinsurance> list = claimDelayinsuranceService.selectClaimDelayinsuranceList(claimDelayinsurance);
        ExcelUtil<ClaimDelayinsurance> util = new ExcelUtil<ClaimDelayinsurance>(ClaimDelayinsurance.class);
        util.exportExcel(response, list, "保险理赔合并数据");
    }

    /**
     * 获取保险理赔合并
详细信息
     */
    @PreAuthorize(hasPermi = "system:delayinsurance:query")
    @GetMapping(value = "/{delayinsuranceId}")
    public AjaxResult getInfo(@PathVariable("delayinsuranceId") Long delayinsuranceId)
    {
        return AjaxResult.success(claimDelayinsuranceService.selectClaimDelayinsuranceById(delayinsuranceId));
    }

    /**
     * 新增保险理赔合并

     */
    @PreAuthorize(hasPermi = "system:delayinsurance:add")
    @Log(title = "保险理赔合并", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClaimDelayinsurance claimDelayinsurance)
    {
        return toAjax(claimDelayinsuranceService.insertClaimDelayinsurance(claimDelayinsurance));
    }

    /**
     * 修改保险理赔合并
     */
//    @PreAuthorize(hasPermi = "system:delayinsurance:edit")
//    @Log(title = "保险理赔合并", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ClaimDelayinsurance claimDelayinsurance) {
        int i;
        try {
            i = claimDelayinsuranceService.updateClaimDelayinsurance(claimDelayinsurance);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除保险理赔合并

     */
    @PreAuthorize(hasPermi = "system:delayinsurance:remove")
    @Log(title = "保险理赔合并", businessType = BusinessType.DELETE)
	@DeleteMapping("/{delayinsuranceIds}")
    public AjaxResult remove(@PathVariable Long[] delayinsuranceIds)
    {
        return toAjax(claimDelayinsuranceService.deleteClaimDelayinsuranceByIds(delayinsuranceIds));
    }

    /**
     * 查询保险理赔
     * */
    @PostMapping("/claimDelayinsuranceList")
    public TableDataInfo claimDelayinsuranceList(@RequestBody ClaimDelayinsuranceVo claimDelayinsuranceVo){
        startPage();
        List<ClaimDelayinsurance> claimDelayinsuranceList;
        try {
            claimDelayinsuranceList = claimDelayinsuranceService.claimDelayinsuranceList(claimDelayinsuranceVo);
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return getDataTable(claimDelayinsuranceList);
    }

    /**
     * 查询待修改的保险理赔信息
     * */
    @PostMapping("/selClaimDelayinsurance")
    public AjaxResult selClaimDelayinsurance(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        UpdateClaimDelayinsurance updateClaimDelayinsurance;
        try {
            updateClaimDelayinsurance = claimDelayinsuranceService.selClaimDelayinsurance(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(updateClaimDelayinsurance);
    }

    /**
     * 修改保险理赔信息
     * */
    @PostMapping("/upClaimDelayinsurance")
    public AjaxResult upClaimDelayinsurance(@RequestBody UpdateClaimDelayinsurance updateClaimDelayinsurance){
        int i;
        try {
            i = claimDelayinsuranceService.upClaimDelayinsurance(updateClaimDelayinsurance);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 延误险保障
     * */
    @PostMapping("/claimDelayinsuranceMaxVo")
    public AjaxResult claimDelayinsuranceMaxVo(HttpServletRequest request) {
        ClaimDelayinsuranceMaxVo claimDelayinsuranceMaxVo;
        try {
            claimDelayinsuranceMaxVo = claimDelayinsuranceService.claimDelayinsuranceMaxVo(request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(claimDelayinsuranceMaxVo);
    }

    /**
     * 延误险保障查询全部
     * */
    @PostMapping("/claimDelayinsuranceMaxList")
    public AjaxResult claimDelayinsuranceMaxList(HttpServletRequest request) {
        List<ClaimDelayinsuranceMaxVo> claimDelayinsuranceMaxVoList;
        try {
            claimDelayinsuranceMaxVoList = claimDelayinsuranceService.claimDelayinsuranceMaxList(request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(claimDelayinsuranceMaxVoList);
    }

    /**
     * 保险理赔详情
     * */
    @PostMapping("/claimDetails")
    public AjaxResult claimDetails(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        ClaimDetailsVo claimDetailsVo;
        try {
            claimDetailsVo = claimDelayinsuranceService.claimDetails(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(claimDetailsVo);
    }

    /**
     * 新增保险理赔信息
     * */
    @PostMapping("/insertInsuranceClaims")
    public AjaxResult insertInsuranceClaims(HttpServletRequest request, @RequestBody TmpInsuranceClaimsVo tmpInsuranceClaimsVo) {
        Customer loginCustomer = loginService.getLoginCustomer(request);
        if (loginCustomer == null) {
            return AjaxResult.error("登录已过期");
        }
        int i;
        try {
            tmpInsuranceClaimsVo.setCustomerId(loginCustomer.getCustomerId().toString());
            i = claimDelayinsuranceService.insertInsuranceClaims(tmpInsuranceClaimsVo);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.success("添加失败");
        }
    }
}
